<template>

  <el-cascader
    :value="value"
    :props="defaultProp"
    :options="options"
    @change="handleChange"
  />

</template>
<script>
// 发请求（获取部门信息），把数据渲染出来
import { getDepartment } from '@/api/department'
import { arr2tree } from '@/utils'
export default {
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      defaultProp: {
        value: 'id',
        label: 'name'
      },
      //   value: [], // 选中的值是数组
      options: []
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    async loadDepartment() {
      const res = await getDepartment()
      const options = arr2tree(res.data, 0)
      console.log(options)
      this.options = options
    },
    handleChange(value) {
      console.log(value) // 它是一个数组
      //   取出最后一个值，回传给父组件
      const id = value[value.length - 1]
      console.log(id)
      this.$emit('input', id)
    }
  }
}
</script>
